package com.yushu.authority.controller;

import com.yushu.authority.pojo.bo.PmUser;
import com.yushu.authority.pojo.vo.PmMenuTreeVO;
import com.yushu.authority.pojo.vo.PmMenuVO;
import com.yushu.authority.pojo.bo.PmMenu;
import com.yushu.authority.service.PmMenuService;
import com.yushu.common.execption.CgException;
import com.yushu.common.anno.LogAop;
import com.yushu.common.shiro.ShiroUtil;
import com.yushu.common.util.pager.Pages;
import com.yushu.common.util.result.Result;

import org.apache.commons.lang3.StringUtils;

//import io.swagger.annotations.Api;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.sound.midi.Soundbank;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;


/**
 * @author test
 * @CreateDate 2020-12-03 15:47:25
 */
//@Api(description = "菜单管理")
@RestController
@RequestMapping("/authority/pmMenu")
public class PmMenuController {
	private Logger logger = Logger.getLogger(PmMenuController.class);

    @Autowired
    private PmMenuService pmMenuService;
    private Map  <String, Object> resultMap;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   //true:允许输入空值，false:不能为空值
    }
    /**
    * 分页条件查询菜单数据
    * 2020-03-06 12:38:55
    * @param tablePage
    * @return
    */
    @LogAop("分页查询菜单")
    @ResponseBody
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Pages page(@RequestBody Pages<PmMenuVO> tablePage) {
        Pages page = pmMenuService.page(tablePage);
        return page;
    }


    /**
     * 查询全部菜单管理数据,用于菜单授权
     * 2019-04-22 20:24:05
     */
    @ResponseBody
    @RequestMapping(value = "/queryAllMenus", method = RequestMethod.GET)
    public List<PmMenu> queryAllMenus() {
        List<PmMenu> list = pmMenuService.queryAllMenus();

        return list;
    }
	/**
    * 查询全部菜单数据
    * 2020-03-06 12:38:55
    */
    @ResponseBody
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    public List<PmMenu> queryList(@RequestBody PmMenu pmMenu) {
        List<PmMenu> list = pmMenuService.selectList(pmMenu);
        return list;
    }

	/**
	* 添加或者更新菜单
	*2020-03-06 12:38:55
	* @param pmMenu
	* @return
	*/
	@LogAop("新增菜单")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public  Result  save(@RequestBody PmMenu pmMenu ) {
		PmUser sessionUser = ShiroUtil.getSessionUser();
	    try {
            logger.info("进入添加菜单方法");
            pmMenu.setCreateTime(new Date());
            pmMenu.setUpdateUserId(sessionUser.getId());
            pmMenuService.insert(pmMenu);
            return Result.success("菜单新增成功");
        }catch (Exception e){
	        e.printStackTrace();
            return Result.error(e.getMessage());
        }
	}


    @LogAop("编辑菜单")
    @RequestMapping(value = "/editor",method = RequestMethod.POST)
    @ResponseBody
    public  Result  editor(@RequestBody PmMenu pmMenu) {
    	PmUser sessionUser = ShiroUtil.getSessionUser();
        try {
            logger.info("进入修改菜单方法");
            pmMenu.setUpdateTime(new Date());
            pmMenu.setUpdateUserId(sessionUser.getId());
            pmMenuService.updateByKey(pmMenu);
            return Result.success("菜单修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }
	
    /**
    *删除菜单数据
    *2020-03-06 12:38:55
    * @param pmMenu 需要删除的对象
    */
    @LogAop("删除菜单")
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(Model model,  @RequestBody PmMenu pmMenu) {
        try {
            pmMenuService.deleteById(pmMenu.getId());
	        logger.info("删除菜单成功");
	        return Result.success("删除菜单成功");
        } catch (CgException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
    *
    * 批量删除菜单数据
    * 2020-03-06 12:38:55
    * @param pmMenus 菜单集合
    */
    @LogAop("批量删除菜单")
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    @ResponseBody
    public Result batchDelete(Model model, @RequestBody List<PmMenu> pmMenus) {
        logger.info("进入到删除菜单数据方法");
        try {
            pmMenuService.deleteBatch(pmMenus);
            return Result.success("批量删除菜单成功");
        } catch (CgException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());

        }
    }

    /**
    * 跳转菜单列表页面
    * 2020-03-06 12:38:55
    */
    @RequestMapping(value = "toPmMenuList", method = RequestMethod.GET)
    public String toPmMenuList(Model model ) {
        model.addAttribute("title","菜单管理");
        model.addAttribute("page","authority/pmMenu/list");
        return "menuPage";
    }

//    /**
//    * 跳转菜单跳转到空白列表页面
//    * 2020-03-06 12:38:55
//    */
//    @RequestMapping(value = "toOpenPmMenuList", method = RequestMethod.GET)
//    public String toOpenPmMenuList(Model model,HttpServletRequest request) {
//        model.addAttribute("title", "菜单管理");
//        model.addAttribute("page", "authority/pmMenu/list");
//        return "openPage";
//    }

//    /**
//    * 跳转菜单新增或修改页面
//    * 2020-03-06 12:38:55
//    */
//    @RequestMapping(value = "toEditor", method = RequestMethod.GET)
//    public String toEditor(Model model, String id , HttpServletRequest request) {
//        if(StringUtils.isNotEmpty(id)){
//	        request.setAttribute("data",  pmMenuService.selectById(id));
//        }
//        model.addAttribute("title", "菜单管理");
//        model.addAttribute("page", "authority/pmMenu/addUpdate");
//        return "openPage";
//    }
    @RequestMapping("checkRepeat")
    @ResponseBody
    public String checkRepeat(@RequestBody Map map ) {
        return pmMenuService.checkRepeat(map);
    }

	/*
    * 校验菜单名称是否重复
    * */
    @ResponseBody
    @RequestMapping(value = "/validatemenuName", method = RequestMethod.POST)
    public String validateOrgName(String menuName, String key) {
        try {
            return pmMenuService.validatemenuName(menuName,key);
        } catch (Exception e) {
            e.printStackTrace();
            return "false";
        }
    }
        /*
        * 校验链接地址是否已经存在
        * */

    @ResponseBody
    @RequestMapping(value = "/validateUrl", method = RequestMethod.POST)
    public String validateUrl(String url, String key) {
        try {
            return pmMenuService.validateUrl(url,key);
        } catch (Exception e) {
            e.printStackTrace();
            return "false";
        }
    }
	


    /**
     * 查询全部菜单管理数据
     * 2019-04-22 20:24:05
     */
    @ResponseBody
    @RequestMapping(value = "/queryMenuTreeByUser", method = RequestMethod.GET)
    public List<PmMenuTreeVO> queryMenuTreeByUser() {
        List<PmMenuTreeVO> list = pmMenuService.queryMenuTree();
        return list;
    }


//    /**
//     * 跳转菜单管理新增或修改页面
//     * 2019-04-22 20:24:05
//     */
//    @RequestMapping(value = "toIcon", method = RequestMethod.GET)
//    public String toIcon(Model model, String id, HttpServletRequest request) {
//        request.setAttribute("data", pmMenuService.selectById(id));
//        model.addAttribute("title", "icon");
//        model.addAttribute("page", "/authority/pmMenu/icon");
//        return "openPage";
//    }
}

